package com.tudouji.project.harvest.service.impl;

import com.flow.service.pojo.ResultParam;
import com.flow.service.pojo.result.FlowResult;
import com.flow.service.process.service.impl.FlowProcessProxy;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.flow.utils.FlowProcessUtil;
import com.tudouji.project.harvest.domain.Freightorder;
import com.tudouji.project.harvest.domain.Procurementcontract;
import com.tudouji.project.harvest.domain.Qaform;
import com.tudouji.project.harvest.mapper.FreightorderMapper;
import com.tudouji.project.harvest.mapper.ProcurementcontractMapper;
import com.tudouji.project.harvest.mapper.QaformMapper;
import com.tudouji.project.harvest.service.IQaformService;
import com.tudouji.project.quality.domain.Qaitems;
import com.tudouji.project.quality.domain.Qarecord;
import com.tudouji.project.quality.mapper.QarecordMapper;
import com.tudouji.project.quality.service.impl.QarecordServiceImpl;
import com.tudouji.project.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.List;

/**
 * 质检单Service业务层处理
 *
 * @author tudouji
 * @date 2021-04-29
 */
@Slf4j
@Service
public class QaformServiceImpl implements IQaformService
{
    @Autowired
    private QaformMapper qaformMapper;
    @Autowired
    private QarecordServiceImpl qarecordService;
    @Autowired
    private QarecordMapper qarecordMapper;
    @Autowired
    private FreightorderMapper freightorderMapper;
    @Autowired
    private ProcurementcontractMapper procurementcontractMapper;
    @Autowired
    private FlowProcessUtil processUtils;
    @Autowired
    private FlowProcessProxy flowProcessProxy;

    /**
     * 查询质检单
     *
     * @param guid 质检单ID
     * @return 质检单
     */
    @Override
    public Qaform selectQaformById(String guid)
    {
        return qaformMapper.selectQaformById(guid);
    }

    /**
     * 查询质检单列表
     *
     * @param qaform 质检单
     * @return 质检单
     */
    @Override
    public List<Qaform> selectQaformList(Qaform qaform)
    {
        return qaformMapper.selectQaformList(qaform);
    }

    /**
     * 新增质检单
     *
     * @param qaform 质检单
     * @return 结果
     */
    @Override
    public int insertQaform(Qaform qaform)
    {
        return qaformMapper.insertQaform(qaform);
    }

    /**
     * 修改质检单
     *
     * @param qaform 质检单
     * @return 结果
     */
    @Override
    public int updateQaform(Qaform qaform)
    {
        return qaformMapper.updateQaform(qaform);
    }

    /**
     * 批量删除质检单
     *
     * @param guids 需要删除的质检单ID
     * @return 结果
     */
    @Override
    public int deleteQaformByIds(String[] guids)
    {
        return qaformMapper.deleteQaformByIds(guids);
    }

    /**
     * 删除质检单信息
     *
     * @param guid 质检单ID
     * @return 结果
     */
    @Override
    public int deleteQaformById(String guid)
    {
        return qaformMapper.deleteQaformById(guid);
    }

    @Override
    public Qaform selectQaformByLinkId(String linkId) {
        return qaformMapper.selectQaformByFoguid(linkId);
    }

    @Override
    public void updateQaformByFoGuid(Qaform qa) {
        qaformMapper.updateQaformByFoGuid(qa);
    }

    @Override
    public AjaxResult getQaDetail(Long id) {
        AjaxResult result = AjaxResult.success();
        Freightorder order = freightorderMapper.selectFreightorderByAutoId(id);
        Qaform qaform = qaformMapper.selectQaformByFoguid(order.getGuid());
        List<Qaitems> qaitems = qarecordService.getQaItemsMain(order);
        List<Qarecord> records = qarecordMapper.selectQarecordListByFoguid(order.getGuid());
        result.put("order", order);
        result.put("qaform", qaform);
        result.put("qaitems", qaitems);
        result.put("records", records);
        return result;
    }


    /**
     *
     * @param guid
     * @param opinion
     * @param sysUser
     * @param fileIds
     * @param orderType 1-货运单 2-领料单 3-入库申请单 4-销售单 5-调拨单
     * @param taskid
     * @return
     */
    public AjaxResult updateOrder(String guid, String opinion, SysUser sysUser, String fileIds, String orderType, Long taskid) {
        if ("1".equalsIgnoreCase(orderType)){
            Freightorder ft = freightorderMapper.selectFreightorderById(guid);
            Procurementcontract contract = procurementcontractMapper.selectProcurementcontractById(ft.getContractid());

            FlowResult result = flowProcessProxy.approval(taskid, fileIds, opinion, ft
                    , sysUser);
            if (result.getMsg().equals(ResultParam.SUCCESS)) {
                Freightorder pb = new Freightorder();
                Qaform qa = qaformMapper.selectQaformByFoguid(guid);
                if (null != qa){
                    pb.setDeductRateByCheck(qa.getDeductrate());
                }
                if (1 == contract.getPaymentmethod() && "2".equalsIgnoreCase(contract.getQatype())){
                    pb.setPaystatus(2);
                }
                pb.setStatus(result.getNextStat());
                pb.setGuid(ft.getGuid());
                this.freightorderMapper.updateFreightorder(pb);

                return AjaxResult.success();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error(result.getMsg());
            }
        }else{
            return AjaxResult.error("其他业务质检未处理！");
        }
    }

    /**
     * 更新或者插入质检单-质检的过程中生成，质检后更新
     * @param qaform
     * @param freightorder
     */
    public void addOrUpdateQaform(Qaform qaform, Freightorder freightorder, SysUser sysUser) {
        if (null == qaform){
            qaform = new Qaform();
            insertQaform(qaform, freightorder, sysUser);
        }else{
            if (StringUtils.isBlank(qaform.getGuid())){
                insertQaform(qaform, freightorder, sysUser);
            }else{
                qaform.setFoguid(freightorder.getGuid());
                qaform.setStatus(2);
                qaform.setLastupdate(DateUtils.getNowDate());
                qaformMapper.updateQaform(qaform);
            }
        }
    }

    private void insertQaform(Qaform qaform, Freightorder freightorder, SysUser sysUser) {
        qaform.setFoguid(freightorder.getGuid());
        qaform.setStatus(1);
        qaform.setCreatedate(DateUtils.getNowDate());
        qaform.setDeductrate(BigDecimal.ZERO);
        String qaCode = "QA" + DateUtils.getCurrentDate("yyyyMMdd") + com.tudouji.common.utils.StringUtils.randStr(9);
        qaform.setQacode(qaCode);
        qaform.setOperatorid(sysUser.getGuid());
        qaform.setGuid(IdUtils.fastSimpleUUID());
        qaformMapper.insertQaform(qaform);
    }
}
